#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    // 安装崩溃捕获
    qInstallMessageHandler([](QtMsgType type, const QMessageLogContext &context, const QString &msg) {
        if (type == QtFatalMsg) {
            QFile file("crash.log");
            if (file.open(QIODevice::Append | QIODevice::Text)) {
                QTextStream stream(&file);
                stream << QDateTime::currentDateTime().toString()
                       << " [FATAL] " << msg << "\n";
                stream << "Location: " << context.file << ":" << context.line << "\n";
                file.close();
            }
        }
    });
    // 启用高DPI缩放（Qt 5.6+）
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    // 使用高DPI像素比（Qt 5.14+）
    QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
    QApplication a(argc, argv);
    // 设置程序图标（需提供图标文件路径）
    a.setWindowIcon(QIcon("icons/icon.png"));
    // Check if config file exists
    if(!QFile::exists("config.ini")) {
        QSettings settings("config.ini", QSettings::IniFormat);
        settings.setValue("Network/CameraIP", "10.201.11.191");
        settings.setValue("Network/CameraPort", 10001);
        // ... set other defaults
        settings.sync();
    }

    try {
        MainWindow w;
        w.show();
        return a.exec();
    } catch (const std::exception& e) {
        QFile crashLog("crash_report.log");
        if (crashLog.open(QIODevice::WriteOnly | QIODevice::Append)) {
            QTextStream stream(&crashLog);
            stream << QDateTime::currentDateTime().toString() << "\n";
            stream << "CRASH: " << e.what() << "\n\n";
        }
        return -1;
    }
    // 设置程序图标（需提供图标文件路径）
    // a.setWindowIcon(QIcon("icons/icon.png"));
    //a.setWindowIcon(QIcon("/home/shenxiuqi/Desktop/camera_test_refined0612/camera_test_refined0607 /AstroCameraSoftware01/icons/icon.png"));  // 使用资源文件
}
